<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item label="运营商" prop="operatoruid">
					<el-select v-model="filters.operatorname" clearable placeholder="请选择运营商">
						<el-option value="0" label="全部">全部</el-option>
						<el-option
								v-for="item in operator"
								:key="item.operatoruid"
								:label="item.operatorname"
								:value="item.operatorname">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.price" clearable placeholder="面额"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="findPageSlowProduct">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="addProduct">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<template>
			<el-table :data="product" highlight-current-row v-loading="loading" max-height="620" border  >
				<el-table-column type="index" width="80" label="编号">
				</el-table-column>
				<el-table-column prop="operator" label="运营商"  >
				</el-table-column>
				<el-table-column prop="price" label="面值"  >
				</el-table-column>
				<el-table-column prop="discount" show-overflow-tooltip  label="折扣"  >
				</el-table-column>
				<el-table-column prop="discountAmount" show-overflow-tooltip  label="支付金额" show-overflow-tooltip >
				</el-table-column>
				<el-table-column prop="status" show-overflow-tooltip  label="状态"   :formatter="formatStatus">
				</el-table-column>
				<el-table-column prop="remark" show-overflow-tooltip  label="备注" >
				</el-table-column>
				<el-table-column prop="createTime" show-overflow-tooltip  label="创建时间" show-overflow-tooltip >
				</el-table-column>


				<el-table-column label="操作"    >
					<template scope="scope"   >
						<el-button size="small"   @click="updateProduct( scope.row)">
							修改
						</el-button>
						<el-button size="small"   @click="updateState(scope.$index, scope.row)">
							{{scope.row.status == 1 ? '禁用' : '启用' }}
						</el-button>
					</template>
				</el-table-column>
			</el-table>
		</template>
		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>

		<!--新增界面-->
		<el-dialog title="新增慢充产品" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="运营商" prop="operator">
					<el-select v-model="addForm.operator" clearable placeholder="请选择运营商">
						<el-option value="0" label="全部">全部</el-option>
						<el-option
								v-for="item in operator"
								:key="item.operatoruid"
								:label="item.operatorname"
								:value="item.operatorname">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="面值" prop="username">
					<el-input v-model="addForm.price" style="width: 200px"  v-on:input="discountChangeAdd"  ></el-input>
				</el-form-item>
				<el-form-item label="折扣" prop="phonenum">
					<el-input v-model="addForm.discount" style="width: 200px"  v-on:input="discountChangeAdd"  ></el-input>
				</el-form-item>
				<el-form-item label="支付金额" prop="password">
					<el-input v-model="addForm.discountAmount" style="width: 200px"  ></el-input>
				</el-form-item>
				<el-form-item label="备注">
					<el-input type="textarea"  style="width: 200px"  v-model="addForm.remark"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

		<el-dialog title="编辑慢充产品" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="运营商" prop="operator">
					<el-select v-model="editForm.operator" clearable placeholder="请选择运营商">
						<el-option value="0" label="全部">全部</el-option>
						<el-option
								v-for="item in operator"
								:key="item.operatoruid"
								:label="item.operatorname"
								:value="item.operatorname">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="面值" prop="username">
					<el-input v-model="editForm.price" style="width: 200px" v-on:input="discountChangeEdit" ></el-input>
				</el-form-item>
				<el-form-item label="折扣" prop="phonenum">
					<el-input v-model="editForm.discount" style="width: 200px" v-on:input="discountChangeEdit" ></el-input>
				</el-form-item>
				<el-form-item label="支付金额" prop="password">
					<el-input v-model="editForm.discountAmount" style="width: 200px"  ></el-input>
				</el-form-item>
				<el-form-item label="备注">
					<el-input type="textarea"  style="width: 200px"  v-model="editForm.remark"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>


</template>
<script>
	import { findPageSlowProduct,updateState,updateProduct,addProduct,getOperatorListPage} from '../../api/api';

    //import NProgress from 'nprogress'
	import moment from 'moment';
	var crypto = require('crypto');
	export default {
		data() {
			return {
				filters: {
					price:'',
					operatorname:''
				},
				isShowSupper:JSON.parse(sessionStorage.getItem("user")).usertype === 2 ? true : false,
				start:0,
				pageSize:10,
				total:0,
				loading: false,
                product:[],
                addFormVisible: false,
                addLoading:false,
				importFormLoading: false,
				importFormRules: {
				},
				operator: [],
				addForm:{
             		price:'',
                    discount:'',
                    discountAmount:'',
                    remark:'',
					operator:''
				},addFormRules:{

				},
                editFormVisible:false,
                editFormRules:{

				},
				editForm:{
                    price:'',
                    discount:'',
                    discountAmount:'',
                    remark:'',
					operator:''
				}
			}
		},
		methods: {
            formatStatus: function (row, column) {
				return row.status == 1 ? '启用' : row.status == 0 ? '禁用 ' : "未知";
			},
			findPageSlowProduct: function () {
				let para = {
					start:this.start,
					pageSize:this.pageSize,
					price:this.filters.price,
					operator:this.filters.operatorname == "0" ? "" : this.filters.operatorname
				};
				this.loading = true;
				findPageSlowProduct(para).then(datas => {
					this.loading = false;
					let { message, code, data } = datas;
					if (code !== "0") {
						this.$message({
							message: message,
							type: 'error'
						});
					} else {
						this.product = data.result;
						this.total = data.totalCount;
					}
				}).catch(e =>{

				});
			},handleCurrentChange:function(val){
				this.start = val - 1;
				this.findPageSlowProduct();
			},
			handleSizeChange:function(val){
				this.pageSize = val;
				this.findPageSlowProduct();
			}
			,updateState:function (index, row){
				this.$confirm('确认启用或禁用产品吗？', '提示', {}).then(() => {
					let para = {
						proId: row.proId,
                        status : row.status == 1 ? 0 : '1'
					};
                    updateState(para).then((datas) => {
						this.listLoading = false;
						let {message, code, data} = datas;
						if (code !== "0") {
							this.$message({
								message: message,
								type: 'error'
							});
						}
						this.$message({
							message: message,
							type: 'info'
						});
						this.findPageSlowProduct();
					});
				});
			},addProduct(){
                this.addFormVisible = true;
			},addSubmit:function(){
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            let para = Object.assign({}, this.addForm);
							addProduct(para).then((datas) => {
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                                this.addFormVisible = false;
                                this.findPageSlowProduct();
                                this.$refs['addForm'].resetFields();
                            }).catch(function (error) {
                                console.log(error);
                            });
                        });
                    }
                });
            },updateProduct(row){
                this.editForm = Object.assign({}, row);
                this.editFormVisible = true;

			},
			editSubmit(){
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            let para = Object.assign({}, this.editForm);
                            updateProduct(para).then((datas) => {
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                                this.editFormVisible = false;
                                this.findPageSlowProduct();
                                this.$refs['addForm'].resetFields();
                            }).catch(function (error) {
                                console.log(error);
                            });
                        });
                    }
                });
			},getOperatorListPage() {
				let para = {
					start: 1,
					pageSize:10,
					operatorname:'',
				};
				this.listLoading = true;
				getOperatorListPage(para).then((datas) => {
					let { message, code, data } = datas;
					if (code !== "0") {
						this.$message({
							message: message,
							type: 'error'
						});
					} else if(data && data.result) {
						this.operator = data.result;
					}
				});
			},
			discountChangeEdit(){
            	if(this.editForm.discount && this.editForm.price){
					this.editForm.discountAmount = (this.editForm.discount * this.editForm.price).toFixed(5);
				}
			},
			discountChangeAdd(){
				if(this.addForm.discount && this.addForm.price){
					this.addForm.discountAmount = (this.addForm.discount * this.addForm.price).toFixed(5);
				}
			}
		},
		mounted() {
			this.getOperatorListPage();
			this.findPageSlowProduct();
		},
		beforeCreate: function (){
//            window.location.reload();
		}
	};

</script>

<style scoped>

</style>
